package dao;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.*;
import java.util.Properties;

public class Base {
    static String driver;
    static String url;
    static String username;
    static String password;

    protected Connection conn = null;
    protected PreparedStatement pstmt = null;
    protected ResultSet rs = null;



    static {
        init();
    }

    static void init() {
        try {
            Properties properties = new Properties();
            String configFile = "database.properties";
            InputStream inputStream = Base.class.getClassLoader().getResourceAsStream(configFile);
            properties.load(inputStream);

            driver = properties.getProperty("driver");
            url = properties.getProperty("url");
            username = properties.getProperty("username");
            password = properties.getProperty("password");

            Class.forName(driver);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public Connection getConn() {
        Connection conn = null;
        try{
            conn = DriverManager.getConnection(url,username,password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    public void closeAll(Connection conn,Statement stmt,ResultSet rs){
        try{
            if(rs!=null){
                rs.close();
            }
            if(stmt!=null){
                stmt.close();
            }
            if(conn!=null){
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
